﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace EfSandbox.Entities.Final.Eod.Mappings {
    class ExchangeMap : EntityTypeConfiguration<Exchange> {
        public ExchangeMap() {
            // Table
            ToTable("fin.eod_exchanges");

            // Primary Key
            HasKey(e => e.ExchangeId);
            Property(e => e.ExchangeId).HasColumnName("exc_id");

            //Attributes
            Property(e => e.ExchangeCode)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_EXC_CD", 1) { IsUnique = true } ))
                .HasColumnName("exc_cd");

            Property(e => e.ExchangeName)
                .IsOptional()
                .IsUnicode(false)
                .HasMaxLength(250)
                .HasColumnName("exc_desc");

            Ignore(e => e.ExchangeLable);

            Ignore(e => e.ExchangeLable2);

            Property(e => e.HasIntraDayProduct)
                .IsOptional()
                .HasColumnName("exc_intraday_fg");

            Property(e => e.IsActive)
                .IsOptional()
                .HasColumnName("exc_active_fg");

            Property(e => e.TimeZoneId)
                .IsOptional()
                .HasColumnName("exc_tiz_id");

            Property(e => e.CountryId)
                .IsOptional()
                .HasColumnName("exc_cnt_id");

            Property(e => e.CurrencyId)
                .IsOptional()
                .HasColumnName("exc_cur_id");

            Property(e => e.ExchangeSuffix)
                .IsOptional()
                .HasMaxLength(25)
                .IsUnicode(false)
                .HasColumnName("exc_suffix_cd");

            // Relationships
            // Timezone
            HasOptional(e => e.TimeZone)
                .WithMany()
                .HasForeignKey(e => e.TimeZoneId);
            
            // Country
            HasOptional(e => e.Country)
                .WithMany()
                .HasForeignKey(e => e.CountryId);

            // Currency
            HasOptional(e => e.Currency)
                .WithMany()
                .HasForeignKey(e => e.CurrencyId);
            
            // Symbol
            HasMany(e => e.Symbols)
                .WithRequired(e => e.Exchange)
                .HasForeignKey(e => e.ExchangeId);
        }
    }
}
